<template>
  <div class="app-card">
    <div class="app-preview">
      <img v-if="app.cover" :src="app.cover" alt="app.appName || '封面图'" />
      <div v-else>
        <span>{{ app.appName }}</span>
      </div>
      <div class="app-overlay">
        <a-space>
          <a-button @click="handleViewChat" v-if="isOwner">查看对话</a-button>
          <a-button @click="handleViewWork" v-if="app?.deployKey">查看作品</a-button>
        </a-space>
      </div>
    </div>
    <div class="app-info">
      <div class="app-info-left">
        <a-avatar :src="app.user?.userAvatar">
          {{ app.user?.userName?.charAt(0) || 'U' }}
        </a-avatar>
      </div>
      <div class="app-info-right">
        <h3>{{ app.appName || '未知应用' }}</h3>
        <p>{{ app.user?.userName || '未知用户' }}</p>
      </div>
    </div>
  </div>
</template>

<script lang="ts" setup>
import { useLoginUserStore } from '@/stores/loginUser'
import { computed } from 'vue'

interface Props {
  app: API.AppVo
  features?: boolean
}

interface Emits {
  (e: 'view-chat', appId: string | number | undefined): void
  (e: 'view-work', app: API.AppVo): void
}

const props = withDefaults(defineProps<Props>(), {
  features: false,
})

const emit = defineEmits<Emits>()

const loginUserStore = useLoginUserStore()
// 是否是本人的应用
const isOwner = computed(() => {
  return props.app.userId === loginUserStore.loginUser?.id
})

const handleViewChat = () => {
  emit('view-chat', props.app.id)
}

const handleViewWork = () => {
  emit('view-work', props.app)
}
</script>

<style scoped>
.app-card {
  background: rgba(255, 255, 255, 0.95);
  border-radius: 16px;
  overflow: hidden;
  box-shadow: 0 8px 32px rgba(0, 0, 0, 0.15);
  backdrop-filter: blur(10px);
  border: 1px solid rgba(255, 255, 255, 0.2);
  transition:
    transform 0.3s,
    box-shadow 0.3s;
  cursor: pointer;
}

.app-card:hover {
  transform: translateY(-8px);
  box-shadow: 0 15px 50px rgba(0, 0, 0, 0.25);
}

.app-preview {
  height: 180px;
  overflow: hidden;
}

.app-preview img {
  width: 100%;
  height: 100%;
  object-fit: cover;
}

.app-overlay {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background: rgba(0, 0, 0, 0.5);
  display: flex;
  justify-content: center;
  align-items: center;
  opacity: 0;
  transition: opacity 0.3s;
}

.app-card:hover .app-overlay {
  opacity: 1;
}

.app-info {
  display: flex;
  align-items: center;
  padding: 16px;
  gap: 16px;
}

.app-info-left {
  flex-shrink: 0;
}

.app-info-right {
  flex: 1;
  min-width: 0;
}

.app-info h3 {
  font-size: 16px;
  font-weight: 600;
  margin: 0 0 4px;
  color: black;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.app-info p {
  font-size: 14px;
  margin: 0;
  color: #666;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
</style>
